<!DOCTYPE>
<html>
<head>
	<meta charset="utf-8">
	<meta name="description" content="">

	<link rel="stylesheet" href="layout.css">
	<title></title>
</head>
<body>
<?php
    require_once 'getdata.php';
    require_once 'config.php';
    
    $data = readData($companies);

    // beim verkauf 0,5% von kurswert aber mindestens 10euro + 0,08% v. kurswert marklerprovision + 5euro transaktionsgeb�hr
    $mesg = arraY(
        "no_results" => 'i found no results :('
    );
    
    echo '<div class="depot-box">';

        echo '<h1>AZUBIENENDRACHEN</h1>';

        $depotValueFull = 50000;
        foreach ($data as $name => $value) {
            $depotValueFull += $value["depot_value"];
        }
        echo '<div class="depot">';
        $depotValueCSSClass = $depotValueFull > 50000 ? 'green' : 'alert';
        
            echo 'Depotwert: <span class="' . $depotValueCSSClass . '">' . number_format($depotValueFull, 2) . '</span> EURO';
        echo '</div>';
        
        foreach ($data as $key => $value) {
            $diffCssClass = $value["diff_full"] > 0 ? 'green' : 'alert';
            $diffPrefix = $value["diff_full"] > 0 ? '+' : '';

            echo '<div class="stock-item">';
                echo $name . '<br>';
                echo 'Stk. | ' . $value["count"] . '<br>';

                 //   $pricepaktie = $actualBuyingCosts / $count - $actualPrice;

                echo 'Akt. Kurs | ' . $value["actualprice"] . " EURO" . '<br>';
                echo 'Diff. p. Aktie | ' . $value["diff_part"]  . " EURO" . '<br>';
                echo 'Diff. Gesamt | <span class="diff ' . $diffCssClass . '">' . $diffPrefix . $value["diff_full"] . " EURO" .'</span><br>';

              //      echo "</br>" . "</br>" ."</br>" ."</br>" ."Depotwert | " . number_format($depotValue, 2) . " EURO";
            echo '</div>';
        }
        
    echo '</div>';
?>
</body>
</html>



<?php
    function readData($companies) {
        $data = array();
        
        foreach ($companies as $company => $info) {
            $exchangeData = getExchangeData($company);
//            $exchangeData = array(
//                "status"        => 200,
//                "name"          => "Nokia",
//                "actualprice"   => 3.20
//            );

            if ($exchangeData["status"] == 200) {
                $buyingCosts = $info["count"] * $info["buyingprice"];
                $actualBuyingCosts = $buyingCosts + (($buyingCosts / 100) / 2) + (((($buyingCosts / 100) / 10) / 10) * 8) + 5;
                $name = $exchangeData["name"];
                $count = $info["count"];
                $actualPrice = $exchangeData["actualprice"];
                //$actualPrice = 122.456;

                $diffPart = round($actualPrice - $actualBuyingCosts / $count, 3);
                $diffFull = round($count * $actualPrice - $actualBuyingCosts, 2);
                $depotValue = round($count * $actualPrice - $actualBuyingCosts);
        // ;)

                 //echo $info["count"] * "2.42" - $actualBuyingCosts;
                $data[$name] = array(
                    "count" => $count,
                    "actualprice" => $actualPrice,
                    "diff_part" => $diffPart,
                    "diff_full" => $diffFull,
                    "depot_value" => $depotValue
                );
                
            } else if ($exchangeData["status"] == 404) {
                echo $mesg["no_results"];
            }
        }
        
        return $data;
    }